package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Contract;
import cn.wolfcode.car.business.mapper.ContractMapper;
import cn.wolfcode.car.business.query.ContractQuery;
import cn.wolfcode.car.business.service.IContractService;
import cn.wolfcode.car.business.util.MinioUtils;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ContractServiceImpl implements IContractService {

    @Autowired
    private ContractMapper contractMapper;


    @Override
    public TablePageInfo<Contract> query(ContractQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Contract>(contractMapper.selectForList(qo));
    }


    @Override
    public void save(Contract contract) {

        contract.setInputTime(new Date());
        contract.setInputUser(ShiroUtils.getUser().getId());
        contractMapper.insert(contract);
    }

    @Override
    public Contract get(Long id) {
        return contractMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Contract contract) {
        Contract contract1 = this.get(contract.getId());
        if (Contract.AUDITSTATE_PASS.equals(contract1.getAuditState()) || contract1.getNullifyState()){
            throw new BusinessException("非法操作");
        }
        contractMapper.updateByPrimaryKey(contract);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            contractMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Contract> list() {
        return contractMapper.selectAll();
    }

    /**
     * 文件上传功能
     * @param file
     * @return
     */
    @Override
    public String uploading(MultipartFile file) throws Exception{
        return MinioUtils.getUrl(file);
    }

    //文件下载
    @Override
    public String downloadFile(Long id) throws FileNotFoundException {
        Contract contract = this.get(id);
        return  contract.getAppendix();
    }

    //审核通过
    @Override
    public void auditON(Long id) {
        Contract contract = this.get(id);
        //审核为审核中,且不为作废
        if (!Contract.AUDITSTATUS_ROGRESS.equals(contract.getAuditState()) || !contract.getNullifyState()){
            throw new BusinessException("非法操作");
        }
        contractMapper.updateAuditStatus(id,Contract.AUDITSTATE_PASS);
    }
    //审核不通过
    @Override
    public void auditOff(Long id) {
        Contract contract = this.get(id);
        if (!Contract.AUDITSTATUS_ROGRESS.equals(contract.getAuditState()) || !contract.getNullifyState()){
            throw new BusinessException("非法操作");
        }
        contractMapper.updateAuditStatus(id,Contract.AUDITSTATE_REJECT);
    }

    //确认盖章
    @Override
    public void affixSealOn(Long id) {
        Contract contract = this.get(id);
        //审核为通过,且不为作废
        if (!Contract.AUDITSTATE_PASS.equals(contract.getAuditState()) || !contract.getNullifyState()){
            throw new BusinessException("非法操作");
        }
        contractMapper.updateAffixSealStatus(id,Contract.AFFIXSEALSTATE_ON);
    }

    //作废合同
    @Override
    public void isNullify(Long id) {
        Contract contract = this.get(id);
        //不盖章,且不为作废,才可以作废合同
        if (contract.getAffixSealState() || !contract.getNullifyState()){
            throw new BusinessException("非法操作");
        }
        contractMapper.updateNullifyStatus(id,Contract.NULLIFYSTATE_OFF);
    }

}
